<template>
  <div class="search_content">
    <div class="tab">
      <div class="border"></div>
      <van-tabs v-model="active" class="tabs">
        <van-tab title="猜你想搜" class="tabs_content">
          <ul>
            <li>二舅回应“精神内耗”</li>
            <li>米奇妙妙屋来头条了</li>
            <li>我们可以穿军装去日本吗sss</li><i class="iconfont icon-re2"></i>
            <li>真的可以穿越时空吗</li>
            <li>小米12spro值得买吗 <i class="iconfont icon-re2"></i></li>
            <li>原神须弥前瞻03 <i class="iconfont icon-xin"></i></li>
            <li>嘎子偷了多少条狗 <i class="iconfont icon-xin"></i></li>
            <li>日本富士山什么时候炸啊啊</li>
          </ul>
        </van-tab>
        <van-tab title="头条热榜" class="tabs_content">内容 2</van-tab>
        <van-tab title="免费小说" class="tabs_content">内容 3</van-tab>
      </van-tabs>
    </div>
    <!--  热点速查-->

    <!-- 历史记录 -->
    <div class="history" v-if="historyList[0]">
      <h4>搜索历史</h4>
      <ul class="history_list">
        <li v-for="(item,index) in historyList" :key="index" @click="searchHistory(item)" class="history_item">
          {{ item }}
        </li>
      </ul>
      <div class="delete" @click="del" >
        <van-icon name="delete-o" /><span>清空搜索历史</span>
      </div>
    </div>
    <!-- 历史记录 -->
    <!-- 搜索发现 -->
    <div class="searchLook">
      <van-nav-bar>
      <template #left>
      <h4>搜索发现</h4>
      </template>
      <template #right>
      <span>隐藏</span>
      </template>
    </van-nav-bar>
    <van-row type="flex" justify="space-between">
    <van-col span="6" offset="1">前端开发</van-col>
    <van-col span="5">郑州大学</van-col>
    <van-col span="5">uniapp</van-col>
    <van-col span="4">React</van-col>
    <van-col span="6" offset="1">南京夏日祭</van-col>
    <van-col span="10" >王者荣耀周年庆</van-col>
    <van-col span="6">和平精英</van-col>
  </van-row>
    </div>
    <!-- 搜索发现 -->

    <!-- 模糊查询列表-->
    <div v-if="value" class="search_info">
      <van-cell-group>
        <van-cell
          class="search_item"
          :title="item.title"
          v-for="item in searchList"
          :key="item._id"
          @click="getSearchList(item.title)"
        ></van-cell>
        <van-cell
          v-for="item in ten"
          :key="item"
          class="search_item"
        ></van-cell>
      </van-cell-group>
    </div>
    <!-- 模糊查询列表-->
  </div>
</template>

<script>
// import { debounce } from "@/utils/debounce";
// import { searchInfo } from "@/api/api";
import { mapState } from "vuex";
export default {
  name: "search",
  props: {
    value: {},
    ten: {},
  },
  data() {
    return {
      active: 0,
      historyList: [],
      //   searchList: [],
    };
  },
  computed: {
    ...mapState(["searchList"]),
  },
  methods: {
    // 把点击搜索过的记录存储到本地
    getSearchList(title) {
      title = title.trim();
      let index = this.historyList.findIndex(v => v ===title)
      if(index >= 0){
        this.historyList.splice(index,1)
      }
      this.historyList.unshift(title);
      localStorage.setItem("historyList", JSON.stringify(this.historyList));
      this.$emit("showSearchList", false,title);
    },
    //清空搜索历史记录
    del(){
      this.historyList = []
      localStorage.removeItem('historyList')
    },
    //点击搜索历史，实现搜索功能，拿到历史记录的文本内容，赋值给搜索框，再调接口
    searchHistory(title){
     this.$emit('searchHistoryList',title)
    }
  },
  created() {
    let historyList = localStorage.getItem("historyList");
    if (historyList) {
      this.historyList = JSON.parse(historyList);
    }
  },
};
</script>

<style lang="less" scoped>
.search_content{
  margin-top: 55px;
}
.van-search__content {
  border-radius: 20px;
}
.icon_left {
  margin: 0 2px;
  font-size: 20px;
}
.border {
  margin: 0 auto;
  width: 340px;
  height: 1px;
  background-color: rgb(213, 212, 212);
}
.tabs {
  .tabs_content {
    font-size: 14px;
    // margin: 15px;

    ul {
      position: relative;
      width: 96%;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
      margin-top: 10px;
      margin-left: 10px;
        >.iconfont{
          position: absolute;
          top: 44px;
          left: 172px;
          color: orange;
        }
    
    }
    ul li {
      width: 45%;
      height: 35px;
      padding-left: 8px;
      line-height: 35px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      // border-right: 1px solid #ccc;
      .icon-xin{
        color: pink;
      }
      .icon-re2{
        color: orange;
        font-weight: 500;
      }
      &:nth-child(even) {
        border: none;
      }
    }
  }
}

.search_info {
  width: 100%;
  position: absolute;
  top: 40px;
  left: 15px;
  z-index: 99;
  .search_item {
    width: 100%;
  }
}
.history {
  padding-left: 15px;
  h4 {
    font-size: 15px;
    margin: 20px 0 5px 7px;
  }
  .delete{
    text-align: center;
    font-size: 14px;
    color: #939393;
    span{
      margin-left: 5px;
    }
  }
  .history_list {
    display: flex;
    flex-wrap: wrap;
    padding-bottom: 10px;
    .history_item {
      width: 40%;
      overflow: hidden;
      text-overflow: ellipsis;
      background-color: #f2f3f5;
      white-space: nowrap;
      display: block;
      text-align: center;
      padding: 5px;
      padding-left: 10px;
      margin: 5px;
      border-radius: 15px;
      font-size: 14px;
    }
  }
}

//搜索发现
.searchLook{
  margin-top: 15px;
  padding: 7px;
  h4{
    font-size: 15px;
  }
  ::v-deep .van-col{
      background-color: #f2f3f5;
      font-size: 14px;
      text-align: center;
      padding: 4px 0;
      border-radius: 15px;
      margin-bottom: 7px;
  }
  ::v-deep .van-nav-bar__right{
    font-size: 12px;
    color: #939393;
  }
}
</style>